package com.game.majiang.algorithm.zhajinhua;

import java.util.Arrays;
import java.util.Vector;
import com.game.majiang.algorithm.niuniu.card;
import com.game.majiang.algorithm.niuniu.carddata;


public class zhajinhualogic {
	private static boolean tonghua(Vector<card> cards)//3张
	{
		boolean flag = true;
		int type = cards.get(0).type.get_type();
		for(int i=0; i<cards.size(); i++)
		{
			if(type != cards.get(i).type.get_type())
			{
				return false;
			}
		}
		return flag;
	}
	private static boolean tonghuashun(Vector<card> cards)//3张
	{
		boolean flag = true;
		if(!tonghua(cards))
		{
			return false;
		}
		if(!shunzi(cards))
		{
			return false;
		}
		return flag;
	}

	/**
	 * 获取到一组牌中val点数的牌有几张
	 * @param val 牌点数
	 * @param cards 一组牌 
	 * @return 几张val牌
	 */
	private static int findpointcount(int val, Vector<card> cards)
	{
		int point = 0;
		if(val <=40)
		{
			point = val%10;
			if(point == 0)
			{
				point = 10;
			}
		}
		else if(val >= 51 && val<=83)
		{
			point = val%10 + 10;
		}
		else
		{
			return 1;
		}
		int count = 0;
		for(int i=0; i<cards.size(); i++)
		{
			int temppoint = 0;
			int tempval = cards.get(i).type.get_value();
			if(tempval <=40)
			{
				temppoint = tempval%10;
				if(temppoint == 0)
				{
					temppoint = 10;
				}
			}
			else if(tempval >= 51 && tempval<=83)
			{
				temppoint = tempval%10 + 10;
			}
			else
			{
				temppoint = 0;
			}
			if(point == temppoint)
			{
				count++;
			}
		}
		return count;
	}
	/**
	 * 
	 * @param cards
	 * @return 返回炸弹的牌value
	 */
	private static int zhadan(Vector<card> cards)//3张
	{
		int flag = 0;
		for(int i=0; i<cards.size(); i++)
		{
			if(findpointcount(cards.get(i).type.get_value(), cards) == 3)
			{
				flag= cards.get(i).type.get_value();
			}
		}
		return flag;
	}

	private static boolean shunzi(Vector<card> cards)//3张
	{
		boolean flag = true;
		int min = 100;
		int arr[] = new int[3];
		for(int i=0; i<cards.size(); i++)
		{
			int po = cards.get(i).type.get_value();
			if(po <=40)
			{
				po = po%10;
				if(po == 0)
				{
					po = 10;
				}
			}
			else if(po >= 51 && po<=83)
			{
				po = po%10 + 10;
			}
			if(po < min)
			{
				min = po;
			}
			arr[i] = po;
		}
		Arrays.sort(arr);
		for(int i=0; i<arr.length; i++)
		{
			if(min+i != arr[i])
			{
				//点数对不上 考虑 q k A的情况
				if(min == 1&&i==1)
				{
					if(min+(14-cards.size()) !=arr[i] )
					{
						//最小是a 1   加上9 不等于10(下一张牌) 
						return false;
					}else{
						min=(14-cards.size());
					}
				}
				else
				{
					return false;
				}
			}
		}
		return flag;
	}
	/**
	 * 
	 * @param cards
	 * @return 对子的点数
	 */
	private static int duizi(Vector<card> cards)
	{
		int flag = 0;
		for(int i=0; i<cards.size(); i++)
		{
			if(findpointcount(cards.get(i).type.get_value(), cards) == 2)
			{
				flag= cards.get(i).type.getZhajinhua_dianshu();
			}
		}
		return flag;
	}
	/**
	 * 比较两组牌的大小
	 * @param cards1
	 * @param cards2
	 * @return
	 */
	public static boolean compareSize(Vector<card> cards1,Vector<card> cards2)
	{
		boolean flag = false;
		if (getPaiXing(cards1).get_value() > getPaiXing(cards2).get_value()) {
			flag = true;
		}else if(getPaiXing(cards1).get_value() == getPaiXing(cards2).get_value()){
			flag = compareSize2(cards1, cards2);
		}
		return flag;
	}
	/**
	 * 比较相同牌型的大小，cards1>cards2 返回true
	 * @param cards1 
	 * @param cards2
	 * @return
	 */
	private static boolean compareSize2(Vector<card> cards1,Vector<card> cards2)
	{
		boolean flag = false;
		cards1 = paixu(cards1);
		cards2 = paixu(cards2);
		if(duizi(cards1)>0){

			carddata danzhangCard = danzhuang(cards1);
			carddata danzhangCard2 = danzhuang(cards2);
			if(danzhangCard.getZhajinhua_dianshu()>danzhangCard2.getZhajinhua_dianshu()){
				flag = true;
				return flag;
			}else if (danzhangCard.getZhajinhua_dianshu()<danzhangCard2.getZhajinhua_dianshu()){
				flag = false;
				return flag;
			}

			if (danzhangCard.get_type()>danzhangCard2.get_type()) {
				flag = true;
				return flag;
			}

		}else{
			for (int i = 0; i < cards1.size(); i++) {
				if (cards1.get(i).type.getZhajinhua_dianshu()>cards2.get(i).type.getZhajinhua_dianshu()) {
					flag = true;
					return flag;
				}else if(cards1.get(i).type.getZhajinhua_dianshu()<cards2.get(i).type.getZhajinhua_dianshu()){
					flag = false;
					return flag;
				}
			}
//			if (cards1.get(0).type.get_type()>cards2.get(0).type.get_type()) {
//				flag = true;
//				return flag;
//			}
		}


		return flag;
	}


	/**
	 * 
	 * 牌型是对子 获取单张的牌
	 * */
	private static carddata danzhuang(Vector<card> cards1) {
		carddata danzhangCard=null;
		int is =0;
		for (int i = 0; i < cards1.size(); i++) {
			is=0;
			for (int j = 0; j < cards1.size(); j++) {
				if(i!=j&&cards1.get(i).type.getZhajinhua_dianshu()==cards1.get(j).type.getZhajinhua_dianshu()){
					is=1;
					break;
				}
			}
			if (is==0) {
				danzhangCard=cards1.get(i).type;
			}
		}


		return danzhangCard;
	}
	/**
	 * 获取到牌型
	 * @param cards 3张牌
	 * @return 牌型
	 */
	public static zhajinhuatype getPaiXing(Vector<card> cards)
	{
		zhajinhuatype type = zhajinhuatype.dan;
		if (zhadan(cards)>0) {
			type = zhajinhuatype.zhadan;//炸弹
		}else if(tonghuashun(cards)){
			type = zhajinhuatype.shunjin;//同花顺
		}else if(tonghua(cards)){
			type = zhajinhuatype.jinhua;//金花
		}else if(shunzi(cards)){
			type = zhajinhuatype.shunzi;//顺子
		}else if(duizi(cards)>0){

			switch (duizi(cards)) {
			case 2:
				type = zhajinhuatype.dui2;//对子
				break;
			case 3:
				type = zhajinhuatype.dui3;//对子
				break;
			case 4:
				type = zhajinhuatype.dui4;//对子
				break;
			case 5:
				type = zhajinhuatype.dui5;//对子
				break;
			case 6:
				type = zhajinhuatype.dui6;//对子
				break;
			case 7:
				type = zhajinhuatype.dui7;//对子
				break;
			case 8:
				type = zhajinhuatype.dui8;//对子
				break;
			case 9:
				type = zhajinhuatype.dui9;//对子
				break;
			case 10:
				type = zhajinhuatype.dui10;//对子
				break;
			case 11:
				type = zhajinhuatype.dui11;//对子
				break;
			case 12:
				type = zhajinhuatype.dui12;//对子
				break;
			case 13:
				type = zhajinhuatype.dui13;//对子
				break;
			case 14:
				type = zhajinhuatype.duiA;//对子
				break;
			default:
				break;
			}


		}
		return type;
	}



	private static Vector<card> paixu(Vector<card> cards)
	{
		Vector<card> temp2 = new Vector<card>();

		temp2.add(cards.get(0));
		temp2.add(cards.get(1));
		temp2.add(cards.get(2));

		// int score[] = {67, 69, 75, 87, 89, 90, 99, 100};
		
			for (int i = 0; i < cards.size() -1; i++){    //最多做n-1趟排序
				for(int j = 0 ;j < cards.size() - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键，这个范围是在逐步缩小的)
					if(cards.get(j).type.getZhajinhua_dianshu() < cards.get(j+1).type.getZhajinhua_dianshu()){  //把小的值交换到后面
						card card = cards.get(j);
						cards.setElementAt(cards.get(j+1), j);
						cards.setElementAt(card, j+1);
						// score[j] = score[j + 1];
						// score[j + 1] = temp;
					}else if(cards.get(j).type.getZhajinhua_dianshu() == cards.get(j+1).type.getZhajinhua_dianshu()){

						if(cards.get(j).type.get_type()>cards.get(j+1).type.get_type()){
							card card = cards.get(j);
							cards.setElementAt(cards.get(j+1), j);
							cards.setElementAt(card, j+1);
						}
					}
				}
			}            
			//		             System.out.print("第" + (i + 1) + "次排序结果：");
			//		              for(int a = 0; a < cards.size(); a++){
			//		                  System.out.print(cards.get(a).type.get_name()+ "\t");
			//		              }
			//		              LogManager.getInstance().Log("");
			
			if(shunzi2(cards)){
				
				card card = cards.get(0);
				cards.setElementAt(cards.get(1), 0);
				cards.setElementAt(card, 1);
				card card2 = cards.get(1);
				cards.setElementAt(cards.get(2), 1);
				cards.setElementAt(card2, 2);
				
				
				
			}
			
		
		//		              System.out.print("最终排序结果：");
		//		             for(int a = 0; a < cards.size(); a++){
		//		                  System.out.print(cards.get(a).type.get_name() + "\t");
		//		         }


		return cards;
	}
	private static boolean shunzi2(Vector<card> cards) {
		boolean flag = true;
		int min = 100;
		int arr[] = new int[3];
		for(int i=0; i<cards.size(); i++)
		{
			int po = cards.get(i).type.get_value();
			if(po <=40)
			{
				po = po%10;
				if(po == 0)
				{
					po = 10;
				}
			}
			else if(po >= 51 && po<=83)
			{
				po = po%10 + 10;
			}
			if(po < min)
			{
				min = po;
			}
			arr[i] = po;
		}
		Arrays.sort(arr);
		int a=0;
		int san=0;
		int er=0;
		for(int i=0; i<arr.length; i++)
		{
			if(arr[i]==1){
				a++;
			}
			if(arr[i]==2){
				er++;
			}
			if(arr[i]==3){
				san++;
			}
		}
		
		if(!(a==1&&san==1&&er==1)){
			return false;
		}
		
		return flag;
	}
}
